home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekikoh Dennoh Club 5
/
Gekikoh Dennoh Club Vol. 5 (Japan).7z
/
Gekikoh Dennoh Club Vol. 5 (Japan) (Track 01).bin
/
docs
/
rakup
/
unify.vl
< prev
Wrap
Lisp/Scheme
|
1998-10-03
|
3KB
|
130 lines
;
; UNIFY.VL : âXâyâVâââïò╧Éöé≡Ägé┴é╜âåâjâtâBâPü[âVâçâô
;
; Copyright (C) 1998 by Makoto Hiroi
;
;
; âåâjâtâBâPü[âVâçâô
;
(defun unify (pattern datum binding)
(cond ((variablep pattern)
(unify-variable pattern datum binding))
((variablep datum)
(unify-variable datum pattern binding))
((and (atom pattern) (atom datum))
(unify-atoms pattern datum binding))
((and (consp pattern) (consp datum))
(unify-pieces pattern datum binding))
(t (clear-binding binding))))
;
; âAâgâÇé╞é╠âåâjâtâBâPü[âVâçâô
;
(defun unify-atoms (pattern datum binding)
(if (equal pattern datum)
binding
(clear-binding binding)))
;
; âèâXâgé╠âåâjâtâBâPü[âVâçâô
;
(defun unify-pieces (pattern datum binding)
(let ((result (unify (car pattern) (car datum) binding)))
(if (eq result 'fail)
'fail
(unify (cdr pattern) (cdr datum) result))))
;
; ò╧Éöé╞é╠âåâjâtâBâPü[âVâçâô
;
(defun unify-variable (var datum binding)
(if (and (boundp var)
(not (eq (symbol-value var) var))) ; Ä⌐ò¬Ä⌐Égé┼é═é╚éó
(unify (symbol-value var) datum binding)
(if (insidep var datum binding)
(clear-binding binding)
(add-binding var datum binding))))
;
; datum é╠Æåé╔ var(ò╧Éö)é¬éáéΘé⌐
;
(defun insidep (var datum binding)
(if (eq var datum)
nil
(inside-sub-p var datum binding)))
(defun inside-sub-p (var datum binding)
(cond ((equal var datum) t)
((atom datum) nil)
((variablep datum)
(if (and (boundp datum)
(not (eq (symbol-value datum) datum)))
(inside-sub-p var (symbol-value datum) binding)))
(t ; list é╠ÅΩìç
(or (inside-sub-p var (car datum) binding)
(inside-sub-p var (cdr datum) binding)))))
;
; ò╧Éöæ⌐ö¢âèâXâgé⌐éτë≡ôÜé≡ò\Īé╖éΘ
;
(defun print-answer (var-list)
(dolist (var var-list)
(format t "~A -> ~A\n" var (variable-value var))))
;
; ò╧Éöé≡Æuè╖é╖éΘ
;
(defun replace-variable (pattern)
(cond
((variablep pattern)
(variable-value pattern))
((atom pattern) pattern)
(t
(cons (replace-variable (car pattern))
(replace-variable (cdr pattern))))))
;
; ò╧ÉöÆlé≡ïüé▀éΘ
;
(defun variable-value (var)
(let (value)
(loop
(unless (boundp var) (return var)) ; ûóæ⌐ö¢
(setq value (symbol-value var)) ; âXâyâVâââïò╧Éöé≡ĵéΦÅoé╖
(cond
((eq var value)
(return value)) ; Ä⌐ò¬Ä⌐Égé¬ôⁿé┴é─éóéΘ
((variablep value)
(setq var value))
((consp value) ; Æåé╔ò╧Éöé¬éáéΘé⌐éαé╡éΩé╚éóé╠é┼Æuè╖é╖éΘ
(return (replace-variable value)))
(t (return value))))))
;
; ò╧ÉöÆlé≡âZâbâgé╖éΘ
;
(defun add-binding (var datum binding)
(set var datum)
(cons var binding))
;
; ò╧Éöé≡âNâèâAé╡é─ 'fail é≡ò╘é╖
;
(defun clear-binding (binding)
(if (consp binding)
(map nil #'makunbound binding))
'fail)
;
; ùvæfé═ò╧Éöé⌐
;
(defun variablep (pattern)
(and (symbolp pattern)
(upper-case-p (char pattern 0))))
; end of file